package demo1_binaryserach;

public class BinarySerach {
    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5,12,32};
        int target = 2;
        int begin = 0;
        int end =arr.length;
        int mid = (begin+ end)/2;

        int index = -1;
        while (true){
            if (arr[mid]==target) {
                index = mid;
                break;
            }
            else {
                if (arr[mid]>target)
                    end = mid-1;
                else
                    begin =mid+1 ;}
            mid = (begin+ end)/2;
        }
        System.out.println("下标为"+index);
        System.out.println("值为"+arr[index]);
    }

}